home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevwddb.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  17.3 KB  |  634 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevwddb.c,v 1.2 2000/09/19 19:00:23 lpd Exp $ */
  20. /*
  21.  * Microsoft Windows 3.n driver for Ghostscript,
  22.  * using device-dependent bitmap.
  23.  *
  24.  * Original version by Russell Lang and Maurice Castro with help from
  25.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  26.  * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  27.  * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  28.  */
  29. #include "gdevmswn.h"
  30.  
  31. /* Make sure we cast to the correct structure type. */
  32. typedef struct gx_device_win_ddb_s gx_device_win_ddb;
  33.  
  34. #undef wdev
  35. #define wdev ((gx_device_win_ddb *)dev)
  36.  
  37. /* Forward references */
  38. private void near win_addtool(P2(gx_device_win_ddb *, int));
  39. private void near win_maketools(P2(gx_device_win_ddb *, HDC));
  40. private void near win_destroytools(P1(gx_device_win_ddb *));
  41.  
  42. /* Device procedures */
  43.  
  44. /* See gxdevice.h for the definitions of the procedures. */
  45. private dev_proc_open_device(win_ddb_open);
  46. private dev_proc_close_device(win_ddb_close);
  47. private dev_proc_map_rgb_color(win_ddb_map_rgb_color);
  48. private dev_proc_fill_rectangle(win_ddb_fill_rectangle);
  49. private dev_proc_tile_rectangle(win_ddb_tile_rectangle);
  50. private dev_proc_copy_mono(win_ddb_copy_mono);
  51. private dev_proc_copy_color(win_ddb_copy_color);
  52.  
  53. /* Windows-specific procedures */
  54. private win_proc_copy_to_clipboard(win_ddb_copy_to_clipboard);
  55. private win_proc_repaint(win_ddb_repaint);
  56. private win_proc_alloc_bitmap(win_ddb_alloc_bitmap);
  57. private win_proc_free_bitmap(win_ddb_free_bitmap);
  58.  
  59. /* The device descriptor */
  60. struct gx_device_win_ddb_s {
  61.     gx_device_common;
  62.     gx_device_win_common;
  63.  
  64.     /* Handles */
  65.  
  66.     HBITMAP FAR hbitmap;
  67.     HDC FAR hdcbit;
  68.     HPEN hpen, *hpens;
  69.     uint hpensize;
  70.     HBRUSH hbrush, *hbrushs;
  71.     uint hbrushsize;
  72. #define select_brush(color)\
  73.   if (wdev->hbrush != wdev->hbrushs[color])\
  74.    {    wdev->hbrush = wdev->hbrushs[color];\
  75.     SelectObject(wdev->hdcbit,wdev->hbrush);\
  76.    }
  77.     HPALETTE hpalette;
  78.     LPLOGPALETTE lpalette;
  79.  
  80.     /* A staging bitmap for copy_mono. */
  81.     /* We want one big enough to handle the standard 16x16 halftone; */
  82.     /* this is also big enough for ordinary-size characters. */
  83.  
  84. #define bmWidthBytes 4        /* must be even */
  85. #define bmWidthBits (bmWidthBytes * 8)
  86. #define bmHeight 32
  87.     HBITMAP FAR hbmmono;
  88.     HDC FAR hdcmono;
  89.     gx_bitmap_id bm_id;
  90. };
  91. private const gx_device_procs win_ddb_procs =
  92. {
  93.     win_ddb_open,
  94.     NULL,            /* get_initial_matrix */
  95.     win_sync_output,
  96.     win_output_page,
  97.     win_ddb_close,
  98.     win_ddb_map_rgb_color,
  99.     win_map_color_rgb,
  100.     win_ddb_fill_rectangle,
  101.     win_ddb_tile_rectangle,
  102.     win_ddb_copy_mono,
  103.     win_ddb_copy_color,
  104.     NULL,            /* draw_line */
  105.     NULL,            /* get_bits */
  106.     win_get_params,
  107.     win_put_params,
  108.     NULL,            /* map_cmyk_color */
  109.     win_get_xfont_procs
  110. };
  111. gx_device_win_ddb far_data gs_mswin_device =
  112. {
  113.     std_device_std_body(gx_device_win_ddb, &win_ddb_procs, "mswin",
  114.             INITIAL_WIDTH, INITIAL_HEIGHT,    /* win_open() fills these in later */
  115.             INITIAL_RESOLUTION, INITIAL_RESOLUTION    /* win_open() fills these in later */
  116.     ),
  117.     {0},            /* std_procs */
  118.     0,                /* BitsPerPixel - not used */
  119.     5000,            /* UpdateInterval (in milliseconds) */
  120.     "\0",            /* GSVIEW_STR */
  121.     0,                /* not a DLL device */
  122.     2,                /* nColors */
  123.     0,                /* mapped_color_flags */
  124.     win_ddb_copy_to_clipboard,
  125.     win_ddb_repaint,
  126.     win_ddb_alloc_bitmap,
  127.     win_ddb_free_bitmap
  128. };
  129.  
  130. /* Open the win_ddb driver */
  131. private int
  132. win_ddb_open(gx_device * dev)
  133. {
  134.     int code = win_open(dev);
  135.     HDC hdc;
  136.  
  137.     if (code < 0)
  138.     return code;
  139.  
  140.     if (wdev->BitsPerPixel > 8)
  141.     return gs_error_limitcheck;    /* don't support 24 bit/pixel */
  142.  
  143.     /* Create the backing bitmap. */
  144.     code = win_ddb_alloc_bitmap((gx_device_win *) dev, dev);
  145.     if (code < 0)
  146.     return code;
  147.  
  148.     /* Create the bitmap and DC for copy_mono. */
  149.     hdc = GetDC(wdev->hwndimg);
  150.     wdev->hbmmono = CreateBitmap(bmWidthBits, bmHeight, 1, 1, NULL);
  151.     wdev->hdcmono = CreateCompatibleDC(hdc);
  152.     if (wdev->hbmmono == NULL || wdev->hdcmono == NULL) {
  153.     win_ddb_free_bitmap((gx_device_win *) dev);
  154.     ReleaseDC(wdev->hwndimg, hdc);
  155.     return win_nomemory();
  156.     }
  157.     SetMapMode(wdev->hdcmono, GetMapMode(hdc));
  158.     SelectObject(wdev->hdcmono, wdev->hbmmono);
  159.     wdev->bm_id = gx_no_bitmap_id;
  160.     ReleaseDC(wdev->hwndimg, hdc);
  161.  
  162.     /* create palette and tools for bitmap */
  163.     if ((wdev->lpalette = win_makepalette((gx_device_win *) dev))
  164.     == (LPLOGPALETTE) NULL)
  165.     return win_nomemory();
  166.     wdev->hpalette = CreatePalette(wdev->lpalette);
  167.     (void)SelectPalette(wdev->hdcbit, wdev->hpalette, NULL);
  168.     RealizePalette(wdev->hdcbit);
  169.     win_maketools(wdev, wdev->hdcbit);
  170.  
  171.     wdev->hdctext = wdev->hdcbit;    /* draw text here */
  172.  
  173.     return 0;
  174. }
  175.  
  176. /* Close the win_ddb driver */
  177. private int
  178. win_ddb_close(gx_device * dev)
  179. {
  180.     /* Free resources */
  181.  
  182.     win_destroytools(wdev);
  183.     DeleteDC(wdev->hdcmono);
  184.     win_ddb_free_bitmap((gx_device_win *) dev);
  185.     DeleteObject(wdev->hpalette);
  186.     DeleteObject(wdev->hbmmono);
  187.     gs_free((char *)(wdev->lpalette), 1, sizeof(LOGPALETTE) +
  188.         (1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  189.         "win_ddb_close");
  190.  
  191.     return win_close(dev);
  192. }
  193.  
  194. /* Map a r-g-b color to the colors available under Windows */
  195. private gx_color_index
  196. win_ddb_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  197.               gx_color_value b)
  198. {
  199.     int i = wdev->nColors;
  200.     gx_color_index color = win_map_rgb_color(dev, r, g, b);
  201.     LPLOGPALETTE lipal = wdev->limgpalette;
  202.     LPLOGPALETTE lpal = wdev->lpalette;
  203.  
  204.     if (color != i)
  205.     return color;
  206.  
  207.     /* We just added a color to the window palette. */
  208.     /* Add it to the bitmap palette as well. */
  209.  
  210.     DeleteObject(wdev->hpalette);
  211.     lpal->palPalEntry[i].peFlags = NULL;
  212.     lpal->palPalEntry[i].peRed = lipal->palPalEntry[i].peRed;
  213.     lpal->palPalEntry[i].peGreen = lipal->palPalEntry[i].peGreen;
  214.     lpal->palPalEntry[i].peBlue = lipal->palPalEntry[i].peBlue;
  215.     lpal->palNumEntries = i + 1;
  216.     wdev->hpalette = CreatePalette(lpal);
  217.     (void)SelectPalette(wdev->hdcbit, wdev->hpalette, NULL);
  218.     RealizePalette(wdev->hdcbit);
  219.     win_addtool(wdev, i);
  220.  
  221.     return color;
  222. }
  223.  
  224. /* Macro for filling a rectangle with a color. */
  225. /* Note that it starts with a declaration. */
  226. #define fill_rect(x, y, w, h, color)\
  227. RECT rect;\
  228. rect.left = x, rect.top = y;\
  229. rect.right = x + w, rect.bottom = y + h;\
  230. FillRect(wdev->hdcbit, &rect, wdev->hbrushs[(int)color])
  231.  
  232.  
  233. /* Fill a rectangle. */
  234. private int
  235. win_ddb_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  236.                gx_color_index color)
  237. {
  238.     fit_fill(dev, x, y, w, h);
  239.     /* Use PatBlt for filling.  Special-case black. */
  240.     if (color == 0)
  241.     PatBlt(wdev->hdcbit, x, y, w, h, rop_write_0s);
  242.     else {
  243.     select_brush((int)color);
  244.     PatBlt(wdev->hdcbit, x, y, w, h, rop_write_pattern);
  245.     }
  246.     win_update((gx_device_win *) dev);
  247.  
  248.     return 0;
  249. }
  250.  
  251. /* Tile a rectangle.  If neither color is transparent, */
  252. /* pre-clear the rectangle to color0 and just tile with color1. */
  253. /* This is faster because of how win_copy_mono is implemented. */
  254. /* Note that this also does the right thing for colored tiles. */
  255. private int
  256. win_ddb_tile_rectangle(gx_device * dev, const gx_tile_bitmap * tile,
  257.       int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  258.                int px, int py)
  259. {
  260.     fit_fill(dev, x, y, w, h);
  261.     if (czero != gx_no_color_index && cone != gx_no_color_index) {
  262.     fill_rect(x, y, w, h, czero);
  263.     czero = gx_no_color_index;
  264.     }
  265.     if (tile->raster == bmWidthBytes && tile->size.y <= bmHeight &&
  266.     (px | py) == 0 && cone != gx_no_color_index
  267.     ) {            /* We can do this much more efficiently */
  268.     /* by using the internal algorithms of copy_mono */
  269.     /* and gx_default_tile_rectangle. */
  270.     int width = tile->size.x;
  271.     int height = tile->size.y;
  272.     int rwidth = tile->rep_width;
  273.     int irx = ((rwidth & (rwidth - 1)) == 0 ?    /* power of 2 */
  274.            x & (rwidth - 1) :
  275.            x % rwidth);
  276.     int ry = y % tile->rep_height;
  277.     int icw = width - irx;
  278.     int ch = height - ry;
  279.     int ex = x + w, ey = y + h;
  280.     int fex = ex - width, fey = ey - height;
  281.     int cx, cy;
  282.  
  283.     select_brush((int)cone);
  284.  
  285.     if (tile->id != wdev->bm_id || tile->id == gx_no_bitmap_id) {
  286.         wdev->bm_id = tile->id;
  287.         SetBitmapBits(wdev->hbmmono,
  288.               (DWORD) (bmWidthBytes * tile->size.y),
  289.               (BYTE *) tile->data);
  290.     }
  291. #define copy_tile(srcx, srcy, tx, ty, tw, th)\
  292.   BitBlt(wdev->hdcbit, tx, ty, tw, th, wdev->hdcmono, srcx, srcy, rop_write_at_1s)
  293.  
  294.     if (ch > h)
  295.         ch = h;
  296.     for (cy = y;;) {
  297.         if (w <= icw)
  298.         copy_tile(irx, ry, x, cy, w, ch);
  299.         else {
  300.         copy_tile(irx, ry, x, cy, icw, ch);
  301.         cx = x + icw;
  302.         while (cx <= fex) {
  303.             copy_tile(0, ry, cx, cy, width, ch);
  304.             cx += width;
  305.         }
  306.         if (cx < ex) {
  307.             copy_tile(0, ry, cx, cy, ex - cx, ch);
  308.         }
  309.         }
  310.         if ((cy += ch) >= ey)
  311.         break;
  312.         ch = (cy > fey ? ey - cy : height);
  313.         ry = 0;
  314.     }
  315.  
  316.     win_update((gx_device_win *) dev);
  317.     return 0;
  318.     }
  319.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
  320. }
  321.  
  322.  
  323. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  324. /* Color = gx_no_color_index means transparent (no effect on the image). */
  325. private int
  326. win_ddb_copy_mono(gx_device * dev,
  327.         const byte * base, int sourcex, int raster, gx_bitmap_id id,
  328.           int x, int y, int w, int h,
  329.           gx_color_index zero, gx_color_index one)
  330. {
  331.     int endx;
  332.     const byte *ptr_line;
  333.     int width_bytes, height;
  334.     DWORD rop = rop_write_at_1s;
  335.     int color;
  336.     BYTE aBit[bmWidthBytes * bmHeight];
  337.     BYTE *aptr = aBit;
  338.  
  339.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  340.  
  341.     if (sourcex & ~7) {
  342.     base += sourcex >> 3;
  343.     sourcex &= 7;
  344.     }
  345.     /* Break up large transfers into smaller ones. */
  346.     while ((endx = sourcex + w) > bmWidthBits) {
  347.     int lastx = (endx - 1) & -bmWidthBits;
  348.     int subw = endx - lastx;
  349.     int code = win_ddb_copy_mono(dev, base, lastx,
  350.                      raster, gx_no_bitmap_id,
  351.                      x + lastx - sourcex, y,
  352.                      subw, h, zero, one);
  353.  
  354.     if (code < 0)
  355.         return code;
  356.     w -= subw;
  357.     }
  358.     while (h > bmHeight) {
  359.     int code;
  360.  
  361.     h -= bmHeight;
  362.     code = win_ddb_copy_mono(dev, base + h * raster, sourcex,
  363.                  raster, gx_no_bitmap_id,
  364.                  x, y + h, w, bmHeight, zero, one);
  365.     if (code < 0)
  366.         return code;
  367.     }
  368.  
  369.     width_bytes = (sourcex + w + 7) >> 3;
  370.     ptr_line = base;
  371.  
  372.     if (zero == gx_no_color_index) {
  373.     if (one == gx_no_color_index)
  374.         return 0;
  375.     color = (int)one;
  376.     if (color == 0)
  377.         rop = rop_write_0_at_1s;
  378.     else
  379.         select_brush(color);
  380.     } else {
  381.     if (one == gx_no_color_index) {
  382.         color = (int)zero;
  383.         rop = rop_write_at_0s;
  384.     } else {        /* Pre-clear the rectangle to zero */
  385.         fill_rect(x, y, w, h, zero);
  386.         color = (int)one;
  387.     }
  388.     select_brush(color);
  389.     }
  390.  
  391.     if (id != wdev->bm_id || id == gx_no_bitmap_id) {
  392.     wdev->bm_id = id;
  393.     if (raster == bmWidthBytes) {    /* We can do the whole thing in a single transfer! */
  394.         SetBitmapBits(wdev->hbmmono,
  395.               (DWORD) (bmWidthBytes * h),
  396.               (BYTE *) base);
  397.     } else {
  398.         for (height = h; height--;
  399.          ptr_line += raster, aptr += bmWidthBytes
  400.         ) {        /* Pack the bits into the bitmap. */
  401.         switch (width_bytes) {
  402.             default:
  403.             memcpy(aptr, ptr_line, width_bytes);
  404.             break;
  405.             case 4:
  406.             aptr[3] = ptr_line[3];
  407.             case 3:
  408.             aptr[2] = ptr_line[2];
  409.             case 2:
  410.             aptr[1] = ptr_line[1];
  411.             case 1:
  412.             aptr[0] = ptr_line[0];
  413.         }
  414.         }
  415.         SetBitmapBits(wdev->hbmmono,
  416.               (DWORD) (bmWidthBytes * h),
  417.               &aBit[0]);
  418.     }
  419.     }
  420.     BitBlt(wdev->hdcbit, x, y, w, h, wdev->hdcmono, sourcex, 0, rop);
  421.     win_update((gx_device_win *) dev);
  422.     return 0;
  423. }
  424.  
  425.  
  426. /* Copy a color pixel map.  This is just like a bitmap, except that */
  427. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  428. private int
  429. win_ddb_copy_color(gx_device * dev,
  430.         const byte * base, int sourcex, int raster, gx_bitmap_id id,
  431.            int x, int y, int w, int h)
  432. {
  433.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  434.  
  435.     if (gx_device_has_color(dev)) {
  436.     switch (dev->color_info.depth) {
  437.         case 8:
  438.         {
  439.             int xi, yi;
  440.             int skip = raster - w;
  441.             const byte *sptr = base + sourcex;
  442.  
  443.             if (w <= 0)
  444.             return 0;
  445.             if (x < 0 || x + w > dev->width)
  446.             return_error(gs_error_rangecheck);
  447.             for (yi = y; yi - y < h; yi++) {
  448.             for (xi = x; xi - x < w; xi++) {
  449.                 int color = *sptr++;
  450.  
  451.                 SetPixel(wdev->hdcbit, xi, yi, PALETTEINDEX(color));
  452.             }
  453.             sptr += skip;
  454.             }
  455.         }
  456.         break;
  457.         case 4:
  458.         {        /* color device, four bits per pixel */
  459.             const byte *line = base + (sourcex >> 1);
  460.             int dest_y = y, end_x = x + w;
  461.  
  462.             if (w <= 0)
  463.             return 0;
  464.             while (h--) {    /* for each line */
  465.             const byte *source = line;
  466.             register int dest_x = x;
  467.  
  468.             if (sourcex & 1) {    /* odd nibble first */
  469.                 int color = *source++ & 0xf;
  470.  
  471.                 SetPixel(wdev->hdcbit, dest_x, dest_y, PALETTEINDEX(color));
  472.                 dest_x++;
  473.             }
  474.             /* Now do full bytes */
  475.             while (dest_x < end_x) {
  476.                 int color = *source >> 4;
  477.  
  478.                 SetPixel(wdev->hdcbit, dest_x, dest_y, PALETTEINDEX(color));
  479.                 dest_x++;
  480.                 if (dest_x < end_x) {
  481.                 color = *source++ & 0xf;
  482.                 SetPixel(wdev->hdcbit, dest_x, dest_y, PALETTEINDEX(color));
  483.                 dest_x++;
  484.                 }
  485.             }
  486.             dest_y++;
  487.             line += raster;
  488.             }
  489.         }
  490.         break;
  491.         default:
  492.         return (-1);    /* panic */
  493.     }
  494.     } else
  495.     /* monochrome device: one bit per pixel */
  496.     {                /* bitmap is the same as win_copy_mono: one bit per pixel */
  497.     win_ddb_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  498.               (gx_color_index) 0,
  499.               (gx_color_index) (dev->color_info.depth == 8 ? 63 : dev->color_info.max_gray));
  500.     }
  501.     win_update((gx_device_win *) dev);
  502.     return 0;
  503. }
  504.  
  505. /* ------ Windows-specific device procedures ------ */
  506.  
  507.  
  508. /* Copy the bitmap to the clipboard. */
  509. private void
  510. win_ddb_copy_to_clipboard(gx_device_win * dev)
  511. {                /* make somewhere to put it and copy */
  512.     HDC hdcbit = wdev->hdcbit;
  513.     HBITMAP bitmap = CreateCompatibleBitmap(hdcbit, dev->width,
  514.                         dev->height);
  515.  
  516.     if (bitmap) {
  517.     /* there is enough memory and the bitmaps OK */
  518.     HDC mem = CreateCompatibleDC(hdcbit);
  519.  
  520.     SelectObject(mem, bitmap);
  521.     BitBlt(mem, 0, 0, dev->width, dev->height,
  522.            hdcbit, 0, 0, SRCCOPY);
  523.     DeleteDC(mem);
  524.     /* copy it to the clipboard */
  525.     OpenClipboard(wdev->hwndimg);
  526.     EmptyClipboard();
  527.     SetClipboardData(CF_BITMAP, bitmap);
  528.     SetClipboardData(CF_PALETTE, CreatePalette(wdev->limgpalette));
  529.     CloseClipboard();
  530.     }
  531. }
  532.  
  533.  
  534. /* Repaint a section of the window. */
  535. private void
  536. win_ddb_repaint(gx_device_win * dev, HDC hdc, int dx, int dy, int wx, int wy,
  537.         int sx, int sy)
  538. {
  539.     BitBlt(hdc, dx, dy, wx, wy, wdev->hdcbit, sx, sy, SRCCOPY);
  540. }
  541.  
  542.  
  543. /* Allocate the backing bitmap. */
  544. private int
  545. win_ddb_alloc_bitmap(gx_device_win * dev, gx_device * param_dev)
  546. {
  547.     HDC hdc;
  548.     int i;
  549.  
  550.     hdc = GetDC(wdev->hwndimg);
  551.     for (i = 0;; i++) {
  552.     wdev->hbitmap = CreateCompatibleBitmap(hdc,
  553.                        param_dev->width, param_dev->height);
  554.     if (wdev->hbitmap != (HBITMAP) NULL)
  555.         break;
  556.     if (i >= 4) {
  557.         ReleaseDC(wdev->hwndimg, hdc);
  558.         return win_nomemory();
  559.     }
  560.     fprintf(stderr, "\nNot enough memory for bitmap.  Halving resolution... ");
  561.     param_dev->x_pixels_per_inch /= 2;
  562.     param_dev->y_pixels_per_inch /= 2;
  563.     param_dev->width /= 2;
  564.     param_dev->height /= 2;
  565.     }
  566.  
  567.     wdev->hdcbit = CreateCompatibleDC(hdc);    /* create Device Context for drawing */
  568.     SelectObject(wdev->hdcbit, wdev->hbitmap);
  569.     ReleaseDC(wdev->hwndimg, hdc);
  570.     return 0;
  571. }
  572.  
  573.  
  574. /* Free the backing bitmap. */
  575. private void
  576. win_ddb_free_bitmap(gx_device_win * dev)
  577. {
  578.     DeleteDC(wdev->hdcbit);    /* must do this first */
  579.     DeleteObject(wdev->hbitmap);
  580. }
  581.  
  582.  
  583. /* ------ Internal routines ------ */
  584.  
  585. #undef wdev
  586.  
  587.  
  588. private void near
  589. win_addtool(gx_device_win_ddb * wdev, int i)
  590. {
  591.     wdev->hpens[i] = CreatePen(PS_SOLID, 1, PALETTEINDEX(i));
  592.     wdev->hbrushs[i] = CreateSolidBrush(PALETTEINDEX(i));
  593. }
  594.  
  595.  
  596. private void near
  597. win_maketools(gx_device_win_ddb * wdev, HDC hdc)
  598. {
  599.     int i;
  600.  
  601.     wdev->hpensize = (1 << (wdev->color_info.depth)) * sizeof(HPEN);
  602.     wdev->hpens = (HPEN *) gs_malloc(1, wdev->hpensize,
  603.                      "win_maketools(pens)");
  604.     wdev->hbrushsize = (1 << (wdev->color_info.depth)) * sizeof(HBRUSH);
  605.     wdev->hbrushs = (HBRUSH *) gs_malloc(1, wdev->hbrushsize,
  606.                      "win_maketools(brushes)");
  607.     if (wdev->hpens && wdev->hbrushs) {
  608.     for (i = 0; i < wdev->nColors; i++)
  609.         win_addtool(wdev, i);
  610.  
  611.     wdev->hpen = wdev->hpens[0];
  612.     SelectObject(hdc, wdev->hpen);
  613.  
  614.     wdev->hbrush = wdev->hbrushs[0];
  615.     SelectObject(hdc, wdev->hbrush);
  616.     }
  617. }
  618.  
  619.  
  620. private void near
  621. win_destroytools(gx_device_win_ddb * wdev)
  622. {
  623.     int i;
  624.  
  625.     for (i = 0; i < wdev->nColors; i++) {
  626.     DeleteObject(wdev->hpens[i]);
  627.     DeleteObject(wdev->hbrushs[i]);
  628.     }
  629.     gs_free((char *)wdev->hbrushs, 1, wdev->hbrushsize,
  630.         "win_destroytools(brushes)");
  631.     gs_free((char *)wdev->hpens, 1, wdev->hpensize,
  632.         "win_destroytools(pens)");
  633. }
  634.